<?php
/**
 * Created by PhpStorm.
 * User: 余智勇
 * Date: 2019/6/25 0025
 * Time: 15:17
 */

namespace app\storage\model;


use think\Model;

class MachineModel extends Model
{
    protected $table = 'smt_machine';
    protected $pk = 'uuid';

    /**
     * @return \think\model\relation\BelongsTo
     * 建立storage表与机器表的连接
     */
    public function Storage()
    {
        return $this->belongsTo('StorageModel', 'storage_uuid', 'uuid')->bind('name');
    }

    /***获取机台（展示）
     * @param $condition
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMachine($condition){
        $where = $condition['where'];
        $pageIndex = $condition['pageIndex'];
        $pageSize = $condition['pageSize'];
        $ret['total'] = $this->where($where)->count();
        $ret['row'] = $this->with('Storage')//belongsTo已经写好了，所以用with建立表间的连接
            ->where($where)->order('machine_id asc')
            ->page($pageIndex,$pageSize)
            ->select();
        return $ret;
    }

    /***获取正在使用的机台（展示）
     * @param $condition
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
       public function getMachineByStatus($condition){
        $where = $condition['where'];
        $where['status']=1;
        $pageIndex = $condition['pageIndex'];
        $pageSize = $condition['pageSize'];
        $ret['total'] = $this->where($where)->count();
        $ret['row'] = $this->with('Storage')//belongsTo已经写好了，所以用with建立表间的连接
            ->where($where)->order('machine_id asc')
            ->page($pageIndex,$pageSize)
            ->select();
        return $ret;
    }

    /**
     * @param $where
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     * 根据条件获取一条机台记录
     */
    public function findMachine($where){
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * @param $data
     * @return int|string
     * 根据条件添加机台
     */
    public function addMachine($data){
        $machineId = $data['machineId'];//机台号
        $storageSelect = $data['storageSelect'];//仓库uuid
        $productLine = $data['productLine'];//产线
        $machineUuid = create_uuid();//创建uuid
        $machineData = ['uuid' => $machineUuid, 'machine_id' => $machineId, 'storage_uuid' => $storageSelect, 'status' => 0, 'feed_uuid' => '', 'product_line' => $productLine];
        $result = $this->insert($machineData);//插入
        return $result;
    }

    /***
     * @param $where
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     * 删除机台
     */
    public function deleteMachine($where)
    {
        $result = $this->where($where)->find()->delete();
        return $result;
    }


}